var app = getApp();
let innerAudioContext = wx.createInnerAudioContext(); //创建音频实例
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xturl: app.myUlife.ulifeUrl + "/",
    children_img_bg: app.myUlife.ulifeUrl + '/wx/children_img_bg.png',
    pendant_icon_img: app.myUlife.ulifeUrl + '/wx/pendant_icon_img.png',
    slide: app.myUlife.ulifeUrl + '/wx/slide_btn_icon.png',//默认图片


    //滑动
    scrollindex: 0,
    totalnum: 2,  //总共页面数
    starty: 0,  //开始的位置x
    endy: 0, //结束的位置y
    margintop: 0,  //滑动下拉距离


    startgray: '../../../images/score_stars_icon_gray.png',//星级灰色
    startyellow: '../../../images/score_stars_icon_yellow.png',//星级黄色

    list: [],//成长记录
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //根据用户ID查询好词好句
    var usid = wx.getStorageSync('usid');
    if (!usid) {
      wx.reLaunch({
        url: '../../pages/login/login'
      })
      return;
    }
    //查询我的阅读记录
    this.getMyAllPoetryreadlog();
  },


  getMyAllPoetryreadlog() {
    var that = this;
    var usid = wx.getStorageSync('usid');
    wx.request({
      url: app.myUlife.ulifeUrl + 'staticlog/getMyAllPoetryreadlog.html',
      data: {
        usid: usid
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        that.setData({
          list: res.data,
          totalnum: res.data.length
        })
      }
    })
  },

  //滑动开始 start
  scrollTouchstart: function (e) {
    let py = e.touches[0].pageY;
    this.setData({
      starty: py
    })
  },
  scrollTouchmove: function (e) {
    let py = e.touches[0].pageY;
    let d = this.data;
    this.setData({
      endy: py,
    })
    if (py - d.starty < 100 && py - d.starty > -100) {
      this.setData({
        margintop: py - d.starty
      })
    }
  },
  scrollTouchend: function (e) {
    let d = this.data;
    if (d.endy - d.starty > 100 && d.scrollindex > 0) {
      this.setData({
        scrollindex: d.scrollindex - 1
      })
    } else if (d.endy - d.starty < -100 && d.scrollindex < this.data.totalnum - 1) {
      this.setData({
        scrollindex: d.scrollindex + 1
      })
    }
    this.setData({
      starty: 0,
      endy: 0,
      margintop: 0
    })
  },

  //向上翻页
  scrollTouchsyg: function () {
    let d = this.data;
    this.setData({
      scrollindex: d.scrollindex - 1
    })
    this.setData({
      starty: 0,
      endy: 0,
      margintop: 0
    })
  },
  //滑动开始 end




  //预览图片
  viewimage: function (e) {
    var picurl = e.target.dataset.picurl;
    var xturl = this.data.xturl;
    wx.previewImage({
      current: xturl + picurl, // 当前显示图片的http链接
      urls: [picurl] // 需要预览的图片http链接列表
    })
  },




  //播放start

  //开始播放
  ksbf: function (e) {
    var index = e.currentTarget.dataset.index;
    let that = this
    var datas=that.data.list;
    
    var musicUrl = that.data.xturl + datas[index].YP; //音频url
    datas[index].playStatus=false;
    that.setData({ list: datas })

    innerAudioContext.src = musicUrl;
    innerAudioContext.autoplay = true
    innerAudioContext.play();
    innerAudioContext.onPlay((res) => {
      innerAudioContext.onTimeUpdate((res) => {
        if (innerAudioContext.duration.toFixed(2) - innerAudioContext.currentTime.toFixed(2) <= 0) {
          that.setStopState(type)
        }
        innerAudioContext.onEnded(() => {
          that.setStopState(type)
        })
      })
    })
  },

  setStopState: function (type) {
    innerAudioContext.seek(0);
    innerAudioContext.stop();
  },


  //结束播放
  canel_handover(e) {
    var index = e.currentTarget.dataset.index;
    innerAudioContext.pause(); //语音暂停
    var datas = this.data.list;
    datas[index].playStatus = true;
    this.setData({ list: datas })
  },




})